package com.example.demo.util;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.channels.MulticastChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

public class Utils {
    //生成随机的用户名
    public static String generateRandomString(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }
    //生成加密密码
    public static String hashPassword(String inputPassword){
        return BCrypt.hashpw(inputPassword,BCrypt.gensalt());
    }
    //验证密码
    public static boolean verifyPassword(String inputPassword,String hashedPassword){
        return BCrypt.checkpw(inputPassword,hashedPassword);
    }
    public static boolean uploadFiles(MultipartFile[] files,String path){
        boolean success = true;
        if(!mkdir(path)){
            //创建目录失败
            System.out.println("创建目录失败！");
            return false;
        }
        if(files.length == 0){
            System.out.println("文件数组长度为0");
            return false;
        }
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            if(file.isEmpty()){
                continue;
            }
            try{
                byte[] bytes = file.getBytes();
                Path filePath = Paths.get(path+"\\"+file.getOriginalFilename());
                System.out.println(path+"\\"+file.getOriginalFilename());
                Files.write(filePath,bytes);
            }catch (IOException E){
                E.printStackTrace();
                success = false;
            }
        }
        return success;
    }
    private static boolean mkdir(String path){
        boolean success = true;
        Path dirPath = Paths.get(path);
        try{
            if(!Files.exists(dirPath)){
                Files.createDirectory(dirPath);
            }
        }catch (IOException E){
            E.printStackTrace();
            success = false;
        }
        return success;
    }
    public static ResponseEntity<Resource> showImg(File file){
        Resource resource = new FileSystemResource(file);
        String contentType;
        try {
            contentType = Files.probeContentType(file.toPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,"inline;filename=\""+resource.getFilename()+"\"");
        headers.add("Content-Type",contentType);
        if(resource.exists()){
            return ResponseEntity.ok()
                    .headers(headers)
//                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(resource);
        }else {
            return ResponseEntity.notFound().build();
        }
    }

}
